package com.osdp.uc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.osdp.common.exception.PlatformException;
import com.osdp.common.util.BeansUtil;
import com.osdp.common.util.ConvertUtil;
import com.osdp.uc.dto.OrganizationDTO;
import com.osdp.uc.entity.UcOrganization;
import com.osdp.uc.mapper.UcOrganizationMapper;
import com.osdp.uc.service.IUcOrganizationService;
import com.osdp.uc.util.Fields;
import com.osdp.uc.vo.UcOrganizationVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class UcOrganizationServiceImpl  extends ServiceImpl<UcOrganizationMapper,UcOrganization> implements IUcOrganizationService {

    @Autowired
    private UcOrganizationMapper ucOrganizationMapper;

    @Override
    public IPage<UcOrganizationVo> pageList(UcOrganizationVo org, Page<UcOrganization> page) {
        UcOrganization paretnOrg =null;
        if(StringUtils.isNotBlank(org.getParentId())) {
            paretnOrg = super.getById(org.getParentId());
        }
        UcOrganization finalEntity = paretnOrg;
        IPage<UcOrganizationVo> pages =  super.page(page, new QueryWrapper<UcOrganization>()
                .eq(StringUtils.isNotBlank(org.getParentId()),Fields.UcOrganizationQ.parentId,org.getParentId())
                .eq(StringUtils.isNotBlank(org.getOrgTypeId()),Fields.UcOrganizationQ.orgTypeId,org.getOrgTypeId())
                .like(StringUtils.isNotBlank(org.getOrgCode()), Fields.UcOrganizationQ.orgCode, org.getOrgCode())
                .like(StringUtils.isNotBlank(org.getOrgName()), Fields.UcOrganizationQ.orgName, org.getOrgName())
        ).convert(track -> {
            UcOrganizationVo vo =  BeansUtil.copy(track, new UcOrganizationVo());
            if(finalEntity != null) {
                vo.setParentName(finalEntity.getOrgName());
            }
            return vo;
        });
        return pages;
    }

    @Override
    public List<UcOrganizationVo> queryOrgTree(Map parameters) {
        return null;
    }



    @Override
    public List<UcOrganization> queryByParentId(String parentId,String orgTypeId) {
        if(StringUtils.isBlank(parentId) == true){
            throw new PlatformException("父节点ID不能为");
        }
        QueryWrapper<UcOrganization> query = new QueryWrapper<>();
//        if(parentId == null || parentId == ""){
//            query.eq(Fields.UcOrganizationQ.parentId, parentId)
//                    .or().isNull(Fields.UcOrganizationQ.parentId);
//        }else {
        query.eq(Fields.UcOrganizationQ.parentId, parentId);
//        }
        query.eq(StringUtils.isNotBlank(orgTypeId),Fields.UcOrganizationQ.orgTypeId,orgTypeId);
        query.orderByAsc(Fields.UcOrganizationQ.sortNo);
        return super.list(query);
    }

    @Override
    public UcOrganizationVo getByOrgId(String id) {
        UcOrganization entity = super.getById(id);
        return ConvertUtil.sourceToTarget(entity,UcOrganizationVo.class);
    }

    @Override
    @Transactional
    public UcOrganizationVo insertOrganization(UcOrganizationVo organizationVo) {
        UcOrganization entity = ConvertUtil.sourceToTarget(organizationVo,UcOrganization.class);
        entity.setLeaf(true);
        super.save(entity);
        if(StringUtils.isNotBlank(entity.getParentId())){
            this.updateOrgLeaf(entity.getParentId(),false);
        }
        return ConvertUtil.sourceToTarget(entity,UcOrganizationVo.class);
    }

    /**
     * 更新父节点
     * @param parentId String
     * @param isLeaf boolean
     */
    public void updateOrgLeaf(String parentId, boolean isLeaf){
        super.update(new UpdateWrapper<UcOrganization>()
                .set(Fields.UcOrganizationQ.isLeaf, isLeaf)
                .eq(Fields.UcOrganizationQ.parentId, parentId));
    }

    @Override
    @Transactional
    public UcOrganizationVo updateOrganization(UcOrganizationVo organizationVo) {
        UcOrganization entity = ConvertUtil.sourceToTarget(organizationVo,UcOrganization.class);
        super.updateById(entity);
        return ConvertUtil.sourceToTarget(entity,UcOrganizationVo.class);
    }

    @Override
    @Transactional
    public boolean deleteOrganization(String id) {
        return super.removeById(id);
    }

    @Override
    @Transactional
    public boolean deleteBatcah(List<String> ids) {
        return super.removeByIds(ids);
    }

    @Override
    public List<OrganizationDTO> queryOrganizationByUserId(String userId){
        List<UcOrganization> list = ucOrganizationMapper.queryOrganizationByUserId(userId);
        return ConvertUtil.sourceToTarget(list, OrganizationDTO.class);
    }
}
